﻿#include "openglwindow.h"
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>

Openglwindow::Openglwindow()
    :m_animating(false)
    ,m_context(0)
    ,m_device(0)
{
    needsInitialize = true;
}
Openglwindow::~Openglwindow()
{
    //删除设备
    delete m_device;
    m_context->makeCurrent(0);
}
void Openglwindow::render(QPainter *painter)
{
    Q_UNUSED(painter);
}

void Openglwindow::initialize()
{
}
void Openglwindow::render()
{
    //清除屏幕， 颜色缓冲区和深度缓冲区和模板缓冲区
//    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void Openglwindow::renderLater()
{
    renderNow();
}
void Openglwindow::renderNow()
{
    if (needsInitialize) {
        //初始化当前上下文的OpenGL函数解析
        initializeOpenGLFunctions();
        initialize();
        needsInitialize = false;
    }

    render();

    if (m_animating)
        renderLater();
}
void Openglwindow::setAnimating(bool animating)
{
    m_animating = animating;

    if (animating)
        renderLater();
}
